package com.yzjs.common.util;


import java.sql.Date;
import java.text.SimpleDateFormat;

import com.yzjs.common.util.conversion.DateUtils;
import org.apache.commons.lang3.StringUtils;

public class MaskUtils {
    public static String maskName(String str) {
        if (str == null || str.isEmpty())
            return "";
        return maskText(str, str.length() / 2, 0);
    }

    public static String maskBankCardNo(String str) {
        return maskText(str, 6, 4);
    }

    public static String maskIdCardNo(String str) {
        return maskText(str, 6, 4);
    }

    public static String maskMobileNo(String str) {
        return maskText(str, 3, 4);
    }

    public static String maskEmail(String str) {
        if (str == null || str.isEmpty())
            return "";
        int index = str.lastIndexOf("@");
        return maskText(str, index / 2, str.length() - index);
    }

    public static String maskDate(Date date) {
        if (date == null)
            return "";
        return DateUtils.date2Str(date, new SimpleDateFormat("yyyy-**-dd"));
    }

    public static String maskText(String str, int prefixLen, int suffixLen) {
        if (str == null || str.isEmpty())
            return "";
        int len = str.length();
        if (len <= prefixLen + suffixLen)
            return str;
        return str.substring(0, prefixLen) + StringUtils.repeat('*', len - prefixLen + suffixLen) + str
                .substring(len - suffixLen);
    }

    public static String maskText(String str) {
        if (StringUtils.isBlank(str))
            return "";
        int maskLen = str.length() / 4;
        if (maskLen < 1)
            maskLen = 1;
        return maskText(str, maskLen, maskLen);
    }
}